import requests
import re
import os

url = 'https://www.pkdoutu.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.56'
}
resp = requests.get(url,headers= headers)
# print(resp.text)

obj = re.compile(r'data-backup="(.*?)"',re.S)
img_list = obj.findall(resp.text)
print(img_list)

# 检查文件夹是否存在，如果不存在则创建
folder_name = 'pictures'
if not os.path.exists(folder_name):
    os.makedirs(folder_name)

# 遍历下载图片
for img_url in img_list:
    name = img_url.split('_')[-1]
    with open(os.path.join(folder_name, name), 'wb') as f:
        f.write(requests.get(img_url, headers=headers).content)
        print(name + '下载完毕')